﻿using NUnit.Framework;
using ScrappyDB.Attributes;
using ScrappyDB.BaseClasses;

namespace ScrappyDB.IntegrationTests.Core
{
    
    
    /// <summary>
    ///This is a test class for DBTest and is intended
    ///to contain all DBTest Unit Tests
    ///</summary>
    [TestFixture]
    public class DeleteOrphanPropertiesTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [DomainName("ScrappyDB_UnitTests"), EntityName("DBOrphan_TestEntity")]
        public class OldEntity 
        {
            [ItemName]
            public string Id { get; set; }

            public string OldName { get; set; }
        }

        [DomainName("ScrappyDB_UnitTests"), EntityName("DBOrphan_TestEntity")]
        public class NewEntity 
        {
            [ItemName]
            public string Id { get; set; }

            [AlternatePropertyName("OldName")]
            public string NewName { get; set; }
        }
 

        [Test]
        public void DeleteOrphanPropertiesTest1()
        {
            var db = new Repository().DB();

            string id = "123";
            string oldname = "OldValue";

            var oldEntity = new OldEntity()
                                {
                                    Id = id,
                                    OldName = oldname
                                };
            db.SaveChanges(oldEntity);

            var newEntity = db.Find<NewEntity>(id,true);
            db.SaveChanges(newEntity);

            Assert.AreEqual(id, newEntity.Id);
            //OldName should have been loaded into NewName
            Assert.AreEqual(oldname, newEntity.NewName);

            db.DeleteOrphanProperties<NewEntity>(id);

            var cleanEntity = db.Find<NewEntity>(id,true);
            //DeleteOrphan should preserve the good values
            Assert.AreEqual(id, cleanEntity.Id);
            Assert.AreEqual(oldname, cleanEntity.NewName);

            var oldCleanEntity = db.Find<OldEntity>(id, true);
            Assert.AreEqual(id, oldCleanEntity.Id);
            //the orphan OldName property should be gone
            Assert.IsNull(oldCleanEntity.OldName);


        }


    }
}
